package com.googlecode.jlogalizer.mapred.util;

import java.io.InputStream;
import java.lang.reflect.Field;
import java.util.Properties;

import org.apache.log4j.Logger;

import com.googlecode.jlogalizer.mapred.log.Constants;


public class MapReducePropertyUtil{
	
	private static final String FileName = "mapreduce.properties";
	private static Logger logger = Logger.getLogger(MapReducePropertyUtil.class);
	
	private Properties mapRedProperties; 
	private static MapReducePropertyUtil singleton;
	
	private MapReducePropertyUtil (String fileName){
		logger.debug("Enter MapReducePropertyUtil - Properties load ");
		if (mapRedProperties==null) { 
			
			mapRedProperties = new Properties();
			InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(fileName);
			
			try{
				mapRedProperties.load(inputStream);
			}
			catch(Exception e){
				e.printStackTrace();
				throw new RuntimeException("Can't load property file - mapreduce.properties from class path");
			}
			logger.debug("Exit MapReducePropertyUtil - Properties load ");
			
		}
	}
	
	public static MapReducePropertyUtil getInstance() {
		if (singleton == null){
			singleton = new MapReducePropertyUtil(FileName);
		}
		return singleton;
	}
	
	public String getValue(String key) {
			return mapRedProperties.getProperty(key);
	}
	
	public String getConstantValue(String key){
		
		    Field field;
		    String value="";
		    Class c = Constants.class;
		    try {
		      field = c.getField(key);
		      value = (String) field.get(c);
		    } catch (NoSuchFieldException e) {
		      logger.debug(e);
		    } catch (SecurityException e) {
		      logger.debug(e);
		    } catch (IllegalAccessException e) {
		      logger.debug(e);
		    }
			return value;
	}
	
	

}